package org.spring.learn.demo.annotation.configures.imports;

import org.spring.learn.project.entity.User;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        // 包里面如果声明了Company和Member这两个类，我才把User对象注册到IoC容器中
        boolean company = registry.containsBeanDefinition("org.spring.learn.project.entity.Company");
        boolean member = registry.containsBeanDefinition("org.spring.learn.project.entity.Member");
        if (company && member) {
            BeanDefinition beanDefinition =new RootBeanDefinition(User.class);

            registry.registerBeanDefinition("user",beanDefinition);
        }
    }
}
